package com.mapper;

import com.pojo.Collections;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CollectionsMapper {

    @Insert("INSERT INTO collections(user_id, note_id, created_at) " +
            "VALUES(#{userId}, #{noteId}, #{createdAt})")
    int insert(Collections collection);

    @Delete("DELETE FROM collections WHERE user_id = #{userId} AND note_id = #{noteId}")
    int delete(@Param("userId") Integer userId, @Param("noteId") Integer noteId);

    @Delete("DELETE FROM collections WHERE user_id = #{userId}")
    int deleteByUserId(Integer userId);

    @Delete("DELETE FROM collections WHERE note_id = #{noteId}")
    int deleteByNoteId(Integer noteId);

    @Select("SELECT * FROM collections WHERE user_id = #{userId} AND note_id = #{noteId}")
    Collections selectByUserAndNote(@Param("userId") Integer userId, @Param("noteId") Integer noteId);

    @Select("SELECT * FROM collections WHERE user_id = #{userId}")
    List<Collections> selectByUserId(Integer userId);

    @Select("SELECT * FROM collections WHERE note_id = #{noteId}")
    List<Collections> selectByNoteId(Integer noteId);

    @Select("SELECT COUNT(*) FROM collections WHERE note_id = #{noteId}")
    int countByNoteId(Integer noteId);

    @Select("SELECT COUNT(*) FROM collections WHERE user_id = #{userId}")
    int countByUserId(Integer userId);

    @Select("SELECT COUNT(*) FROM collections WHERE user_id = #{userId} AND note_id = #{noteId}")
    int exists(@Param("userId") Integer userId, @Param("noteId") Integer noteId);
}